<!-- 生产溯源信息 -->
<template>
  <BaseMainSlot>
    <template v-slot:searchbox>
      <el-collapse v-model="activeName" accordion @change="handleChange">
        <el-collapse-item name="1">
          <template slot="title">
            <div class="select-btns search-btns" @click.stop="changeSelect()">
              <i class="el-icon-circle-check"></i>
              <p v-if="activeName === '1'">收起筛选</p>
              <p v-if="activeName === ''">展开筛选</p>
            </div>
          </template>
          <div class="search-btn search-btns" @click.stop="() => {
            currentPage = 1; queryPageList()
          }">
            <i class="el-icon-circle-check"></i>
            <p>查询</p>
          </div>
          <div class="select-box-content">
            <div class="select-box-content-name">批次编号：</div>
            <el-select filterable clearable v-model="searchObj.productionBatchCode">
              <template v-for="batchitem in batchList">
                <el-option v-if="batchitem.status == 'COMPLETED'" :key="batchitem.productionBatchId"
                  :label="batchitem.mnecode" :value="batchitem.productionBatchCode" />
              </template>
            </el-select>
          </div>
          <div class="select-box-content">
            <div class="select-box-content-name">养殖品种：</div>
            <el-select filterable clearable v-model="searchObj.fishVarietyId" allow-create>
              <el-option v-for="item of fishVarietyList" :key="item.fishVarietyId + 'variety'" :label="item.varietyName"
                :value="item.fishVarietyId" />
            </el-select>
          </div>
        </el-collapse-item>
      </el-collapse>
    </template>
    <template v-slot:content>
      <div class="baseInfoList flex">
        <div class="baseInfo flex" v-for="fishData, index in  tableData " :key="index" @click="jumpFishInfo(fishData); setTime()">
          <div class="fishImg">
            <div class="fishImgBox">
              <img :src="fishData.pictureImg" alt="">
            </div>
          </div>
          <div class="info" style="padding-left: 30px;">
            <div>批次编号：<span style="color: #999999;">{{ fishData.mnecode }}</span></div>
            <div>品种名称：<span style="color: #999999;">{{ fishData.varietyName }}</span></div>
            <div>养殖主体：<span style="color: #999999;">{{ homeBaseName }}</span></div>
            <div>主体地址：<span style="color: #999999;">{{ address }}</span></div>
          </div>
          <div class="QRCode">
            <div class="imgBox">
              <Qrcode v-if="showTable"
                :apkUrl="`http://fishery.ssagroup.cn:8080/h5/#/?traceSourceFlag=false&batchId=${fishData.productionBatchId}&homeBaseId=${homeBaseId}`">
              </Qrcode>
            </div>
            <div>扫码溯源</div>
          </div>
        </div>
      </div>
      <Pagination :page-size="pageSize" :current-page="currentPage" :total-size="totalSize"
        @changeCurrentPage="changeCurrentPage" />
      <el-dialog title="溯源信息" :visible.sync="dialogVisible" width="95%" :before-close="handleClose">
        <div>
          <TraceabilityMsgParticulars v-if="showTable" :batchParticularsInfo="batchInfo" />
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">关 闭</el-button>
          <!-- <el-button type="primary" @click="dialogVisible = false">确 定</el-button> -->
        </span>
      </el-dialog>
    </template>
  </BaseMainSlot>
</template>

<script>
import Pagination from "@/components/Pagination";
import BaseMainSlot from "@/components/BaseMainSlot/kong-new.vue";
import Qrcode from "@/components/qrcode";
import TraceabilityMsgParticulars from "@/components/traceabilityMsgParticulars";  // 
import subjectInfoApi from "@/api/traceability/basicsInfo/subjectInfo";            // 主体信息

import traceabilityMsgApi from "@/api/traceability/basicsInfo/traceabilityMsg";
import FishVarietyApi from "@/api/basic-info-manage/fishVariety"
import BatchMangeApi from "@/api/batch-mange";
import { mymixin } from "@/utils/mixin";
import { mapGetters } from "vuex";

import DocumentApi from "@/api/document";
import { blobToBase64 } from "@/utils/convert";
export default {
  name: "StaffManage",
  components: {
    Pagination,
    BaseMainSlot,
    Qrcode,
    TraceabilityMsgParticulars
  },
  mixins: [mymixin],
  data () {
    return {
      address:'', // 主体地址
      dialogVisible: false,
      homeBaseName: sessionStorage.getItem('homeBaseName'),
      homeBaseId: sessionStorage.getItem('homeBaseId'),
      activeName: '',
      currentPage: 1,
      totalSize: 1,
      pageSize: 4,
      // 查询条件
      searchObj: {},
      options: [],
      fishVarietyList: [],
      batchList: [],
      batchInfo: {},
      showTable: true
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission",
    }),
  },
  watch: {},
  created () {
  },
  mounted () {
    this.queryPageList()
    this.queryAllList()
    this.queryByHomeBaseId () // 主体信息
  },
  methods: {
    setTime () {
      this.showTable = false
      setTimeout(() => {
        this.showTable = true
      }, 10)
    },
    handleClose (done) {
      this.batchInfo = {}
      done()
    },
    jumpFishInfo (data) {
      this.batchInfo = {
        productionBatchId: data.productionBatchId,
        productionBatchCode: data.productionBatchCode,
      }
      this.dialogVisible = true
    },
    handleChange () {
      this.activeName ? this.activeName = '1' : this.activeName = ''
    },
    queryAllList () {
      FishVarietyApi.queryByCondition().then(res => {
        this.fishVarietyList = res.data
      })
      BatchMangeApi.queryByCondition({
        status: "COMPLETED"
      }).then(res => {
        this.batchList = res.data;
      })
    },

    async fileDownload (fileId) {
      let res = fileId && await DocumentApi.fileDownload({ fileId })
      console.log(res, '---')
      let img = await blobToBase64(res.data)
      return img
    },
    //查询溯源信息列表
    queryPageList () {
      let params = {
        page: this.currentPage,
        size: 4,
        entity: {
          status: 'COMPLETED',
          ...this.searchObj
        }
      };
      traceabilityMsgApi.queryPageByCondition(params).then(res => {
        this.setTime()
        if (res.success === true) {
          res.data.content.forEach(async (item) => {
            if (item.pictureId) {
              item['pictureImg'] = this.$baseurl.webServiceUrl + 'base/files/preview?fileId=' + item.pictureId
            } else {
              item['pictureImg'] = require("@/assets/fish.jpg")
            }
          })
          this.totalSize = res.data.totalElements;
          this.tableData = res.data.content;
        }
        this.routerParams = {}
      });
    },
    // 获取主体信息
    queryByHomeBaseId () {
      subjectInfoApi.queryByHomeBaseId({homeBaseId: sessionStorage.getItem('homeBaseId')}).then(res => {
        if (res && res.data) {
          this.address=res.data.address
        }
      })
    },
    changeCurrentPage (e) {
      this.currentPage = e
      this.queryPageList()
    }
  },
};
</script>

<style lang="scss" scoped>
.flex {
  display: flex;
  justify-content: space-between;
  flex-flow: row wrap
}

.baseInfoList {
  height: calc(100% - 40px);
  width: 100%;
  padding: 15px;

  .baseInfo {
    width: calc(50% - 20px);
    height: 240px;
    box-sizing: border-box;
    margin: 10px 10px;
    background: #ffffff;
    align-items: center;
    justify-content: flex-start;
    position: relative;
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0px 0px 10px 0px #49c8ff;
    cursor: pointer;

    .fishImg {
      width: 260px;
      height: 100%;
      background: #cecece;
      background-image: url("~@/assets/haibg.png");

      .fishImgBox {
        width: calc(100% - 40px);
        height: calc(100% - 40px);
        margin: 20px;
        border-radius: 8px;
        box-sizing: border-box;
        background: #3333335c;
        overflow: hidden;

        img {
          width: 100%;
          height: 100%;
          box-shadow: 0 0 10px 0 #cecece;
        }
      }
    }

    .info {
      text-align: left;
      color: #333;
      padding: 0 10px;
      font-size: 16px;
      line-height: 30px;
      width: 300px;
    }

    .QRCode {
      position: absolute;
      top: 0;
      right: 30px;
      width: 150px;
      height: 100%;
      text-align: center;
      color: #333;
      font-size: 16px;
      padding: 30px 0;
      background: #d7eaff;

      .imgBox {
        height: calc(130px);
        width: calc(130px);
        margin: 0 auto;
        margin-bottom: 10px;
        padding: 5px;
        background: #ffffff;
        box-sizing: border-box;

        img {
          width: calc(100%);
          height: calc(100%);
        }
      }
    }
  }
}

::v-deep .el-dialog {
  margin-top: 0 !important;

  .el-dialog__body {
    height: calc(100vh - 120px);
    overflow-y: auto;
  }
}
</style>
